package com.example.mgs.listeners;

import com.example.common.constants.ActivityConstants;
import com.example.common.constants.RoleConstants;
import com.example.common.domain.Messages.UserActivityMessage;
import com.example.mgs.Service.ActivityService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@Slf4j
public class ActivityListener {

    @Autowired
    private ActivityService activityService;

    // 教师活跃度消费者（自动确认）
    @RabbitListener(queues = ActivityConstants.TEACHER_ACTIVITY_QUEUE)
    public void processTeacherActivity(UserActivityMessage message) {
        log.info("接收教师活跃度消息: {}", message);
        try {
            activityService.processActivity(message, RoleConstants.TEACHER);
            log.info("教师活跃度消息处理成功");
        } catch (Exception e) {
            log.error("教师活跃度消息处理失败: {}", message, e);
            // 异常会自动触发重试（根据配置）
            throw e; // 重新抛出异常，触发自动nack
        }
    }

    // 学生活跃度消费者（自动确认）
    @RabbitListener(queues = ActivityConstants.STUDENT_ACTIVITY_QUEUE)
    public void processStudentActivity(UserActivityMessage message) {
        log.info("接收学生活跃度消息: {}", message);
        try {
            activityService.processActivity(message, RoleConstants.STUDENT);
            log.info("学生活跃度消息处理成功");
        } catch (Exception e) {
            log.error("学生活跃度消息处理失败: {}", message, e);
            throw e;
        }
    }

}
